<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>03 key的作用</title>
  </head>
  <body>
    <div id="app">
      <input type="text" v-model="newdata" />
      <input type="button" value="添加数据" @click="add" />
      <ul>
        <li v-for="(item, index) in seafood" :key="item.id">
          <input type="checkbox" />{{ item.title }}
        </li>
      </ul>
    </div>
  </body>
  <script src="../node_modules/vue/dist/vue.js"></script>
  <script>
    //key值必须是唯一且稳定的值
    new Vue({
      el: "#app",
      data: {
        newdata: "",
        seafood: [
          {
            id: 1,
            title: "濑尿虾",
            price: "30",
          },
          {
            id: 2,
            title: "大闸蟹",
            price: "90",
          },
          {
            id: 3,
            title: "鲈鱼",
            price: "90",
          },
        ],
      },

      //方法
      methods: {
        add() {
          console.log(this.newdata, 888);
          let obj = {
            title: this.newdata,
            price: 80,
          };
          this.seafood.unshift(obj);
        },
      },
    });
  </script>
</html>
